<template>
  <div class="pb30">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
      class="fl"
      @submit.native.prevent
    >
      <el-form-item label="群组名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入群组名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-button
          size="mini"
          type="success"
          @click="handleAdd"
          v-hasPermi="['system:group:add']"
        >
          新增群组
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column
        label="操作"
        align="center"
        width="130"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <span class="blue cp mr10" @click="handleEdit(scope.row)">
            <i class="el-icon-edit"></i> 修改
          </span>

          <span class="blue cp" @click="handleDel(scope.row)">
            <i class="el-icon-delete"></i> 删除
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="群组名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="微信群名称"
        align="center"
        prop="wxGroupList"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span
            v-for="(item, index) in scope.row.wxGroupList"
            :key="index"
            class="mb10"
          >
            <template v-if="index === 0">{{ item }}</template>
            <template v-else>、{{ item }}</template>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加修改 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="92px">
        <el-form-item label="群组名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入群组名称，1~10个字符"
            maxlength="10"
          />
        </el-form-item>
        <el-form-item label="微信群名称" prop="groupString">
          <el-select
            v-model="form.groupString"
            placeholder="请选择微信群名称"
            clearable
            multiple
            filterable
            class="w100"
          >
            <el-option
              v-for="(item, index) in groupOption"
              :key="index"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          size="small"
          :loading="btnLoading"
          @click="submitForm"
        >
          保 存
        </el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  groupList,
  groupInfo,
  addGroup,
  editGroup,
  delGroup,
} from "@/api/project/group";
import { getGroupName } from "@/api/project/send";

export default {
  data() {
    return {
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      list: [],

      title: "",
      open: false,

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "请输入群组名称", trigger: "blur" }],
        groupString: [
          { required: true, message: "请选择微信群名称", trigger: "change" },
        ],
      },

      groupOption: [],
      btnLoading: false,
    };
  },

  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      groupList(this.queryParams)
        .then((res) => {
          if (res.rows.length != 0) {
            res.rows.forEach((item) => {
              item.wxGroupList = JSON.parse(item.wxGroups);
            });
          }
          this.list = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 获取群组名称
    getGroup() {
      getGroupName().then((res) => {
        this.groupOption = res.data;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        wxGroups: undefined,
        groupString: [],
        name: undefined,
        groupId: undefined,
      };
      this.resetForm("form");
      this.groupOption = [];
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getGroup();
      this.open = true;
      this.title = "新增群组";
    },

    // 编辑
    handleEdit(row) {
      this.reset();
      this.getGroup();
      groupInfo(row.groupId).then((res) => {
        this.form = res.data;
        this.$set(this.form, "groupString", JSON.parse(res.data.wxGroups));
        this.open = true;
        this.title = "修改群组";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;

          var param = {
            groupId: this.form.groupId,
            name: this.form.name,
            wxGroups: JSON.stringify(this.form.groupString),
          };
          if (this.form.groupId != null) {
            editGroup(param)
              .then((response) => {
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.btnLoading = false;
              });
          } else {
            addGroup(param)
              .then((response) => {
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.btnLoading = false;
              });
          }
        }
      });
    },

    // 删除
    handleDel(row) {
      const groupId = row.groupId;
      this.$modal
        .confirm('是否确认删除群组名称为"' + row.name + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delGroup(groupId);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style>
.fl {
  float: left;
}
.fr {
  float: right;
}
.w100 {
  width: 100%;
}

.el-dialog__header {
  border-bottom: 1px solid #ddd;
}
.blue {
  color: #1890ff;
}
.mr10 {
  margin-right: 10px !important;
}
.mb10 {
  margin-bottom: 10px !important;
}
.cp {
  cursor: pointer;
}
.pb30 {
  padding-bottom: 30px;
}
</style>
